package com.enfry.common.util.xml;

import java.io.File;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/** 
 * XML解析 
 * @author  hecongqi
 * @revise 	oujiangbo  
 * @version 创建时间：2011-9-26 上午10:22:45 
 */
public class XmlUtil {
	
	public static Element getRoot(File file) throws Exception {
		if(file == null){
			return null;
		}
		SAXReader saxReader = new SAXReader();
		try {
			Document document = saxReader.read(file);
			Element root = document.getRootElement();
			return root;
		} catch (DocumentException e) {
			throw e;
		}
	}
	//如果BufferedReader 已经读过，dom4j再读的时候就不是从头开始读，所以报错。
	public static Element getRoot(InputStream in) throws Exception{
		SAXReader saxReader = new SAXReader();
		try {
			Document document = saxReader.read(in);
			Element root = document.getRootElement();
			return root;
		} catch (DocumentException e) {
			throw e;
		}
	}
	
	public static Element getRoot(String str) throws Exception{
		SAXReader saxReader = new SAXReader();
		Reader reader = new StringReader(str);
		try {
			Document document = saxReader.read(reader);
			Element root = document.getRootElement();
			return root;
		} catch (DocumentException e) {
			throw e;
		}
	}
	
	public static XmlElement parse(String str) throws Exception{
		try {
			Element e = getRoot(str);
			return parse(e);
		} catch (Exception err) {
			throw err;
		}
	}
	
	public static XmlElement parse(File file) throws Exception{
		try {
			Element e = getRoot(file);
			return parse(e);
		} catch (Exception err) {
			throw err;
		}
	}
	
	public static XmlElement parse(InputStream in) throws Exception{
		try {
			Element e = getRoot(in);
			return parse(e);
		} catch (Exception err) {
			throw err;
		}
	}
	
	public static XmlElement parse(Element e){
		if(e==null){
			return null;
		}
		XmlElement xe = new XmlElement();
		parse(e,xe);
		return xe;
	}
	
	//Element --> XmlElement
	public static void parse(Element e,XmlElement e2){
		e2.setKey(e.getName());
		e2.setVal(e.getText());
		
		@SuppressWarnings("unchecked")
		List<Element> sons = e.elements();
		for(Element element : sons){
			XmlElement son = new XmlElement();
			e2.getSons().add(son);
			parse(element,son);
		}
	}
	@SuppressWarnings({ "unchecked", "rawtypes" })
	private static String pack(Map<String,Object> map){
		if(map==null){
			return null;
		}
		StringBuffer sb = new StringBuffer();
		Set<String> keySet = map.keySet();
		for(Iterator<String> it = keySet.iterator();it.hasNext();){
			String key = it.next();
			Object value = map.get(key);
			if(value instanceof List){
				for(Object obj:(List)value){
					sb.append("<").append(key).append(">").append("\n");
					sb.append(pack((Map<String, Object>)obj));
					sb.append("</").append(key).append(">").append("\n");
				}
			}else if(value instanceof Map){
				sb.append("<").append(key).append(">").append("\n");
				sb.append(pack((Map<String, Object>)value));
				sb.append("</").append(key).append(">").append("\n");
			}else if(value instanceof String){
				sb.append("<").append(key).append(">");
				sb.append(value);
				sb.append("</").append(key).append(">").append("\n");
			}else if(value instanceof Integer){
				sb.append("<").append(key).append(">");
				sb.append(value);
				sb.append("</").append(key).append(">").append("\n");
			}
		}
		return sb.toString();
	}
	
	public static String pack(String code,Map<String,Object> map){
		if(code==null || code.equals("")){
			code ="UTF-8";
		}
		StringBuffer sb = new StringBuffer();
		sb.append("<?xml version=\"1.0\" encoding=\"").append(code).append("\"?>");
		sb.append(pack(map));
		return sb.toString();
	}
}




